//Sava was here
// movement vars
var xpos = 100;
var ypos = 100;
var xspeed = 1;
var yspeed = 0;
var maxSpeed = 5;
var loopspeed = 10;
var pause = "n";

// score vars
var energy = 100;
var time = 0;

// car71 vars
var x71pos = 1200;
var y71pos = 130;

// car72 vars
var x72pos = 200;
var y72pos = 210;

// car73 vars
var x73pos = 600;
var y73pos = 290;

// car74 vars
var x74pos = 1000;
var y74pos = 370;

// car75 vars
var x75pos = 100;
var y75pos = 450;


// car77 vars
var x77pos = 1000;
var y77pos = 370;



// boundary
var minx = 0;
var miny = 110;
var maxx = 1165; // 10 pixels for character's width
var maxy = 455; // 10 pixels for character's width

// controller vars
var upPressed = 0;
var downPressed = 0;
var leftPressed = 0;
var rightPressed = 0;

function slowDownX()
{
  if (xspeed > 0)
    xspeed = xspeed - 1;
  if (xspeed < 0)
    xspeed = xspeed + 1;
}

function slowDownY()
{
  if (yspeed > 0)
    yspeed = yspeed - 1;
  if (yspeed < 0)
    yspeed = yspeed + 1;
}

function gameLoop()
{
  if (pause == "n")
  {
  // change position based on speed
  xpos = Math.min(Math.max(xpos + xspeed,minx),maxx);
  ypos = Math.min(Math.max(ypos + yspeed,miny),maxy);

  // or, without boundaries:
  // xpos = xpos + xspeed;
  // ypos = ypos + yspeed;

  // change actual position
  document.getElementById('character').style.left = xpos;
  document.getElementById('character').style.top = ypos;

  if ((xpos>(x71pos-120) && xpos<(x71pos+120)) && (ypos>(y71pos-72) && ypos<(y71pos+72))) energy= energy- 1;
  if ((xpos>(x72pos-120) && xpos<(x72pos+120)) && (ypos>(y72pos-72) && ypos<(y72pos+72))) energy= energy- 1;
  if ((xpos>(x73pos-120) && xpos<(x73pos+120)) && (ypos>(y73pos-72) && ypos<(y73pos+72))) energy= energy- 1;
  if ((xpos>(x74pos-120) && xpos<(x74pos+120)) && (ypos>(y74pos-72) && ypos<(y74pos+72))) energy= energy- 1;
  if ((xpos>(x75pos-120) && xpos<(x75pos+120)) && (ypos>(y75pos-72) && ypos<(y75pos+72))) energy= energy- 1;
  if ((xpos>(x77pos-120) && xpos<(x77pos+120)) && (ypos>(y77pos-72) && ypos<(y77pos+72))) energy= energy- 4;


  x71pos = x71pos - 3;
  document.getElementById('c71').style.left = x71pos;
  if (x71pos < -400) x71pos = 1300;

  x72pos = x72pos - 5;
  document.getElementById('c72').style.left = x72pos;
  if (x72pos < -400) x72pos = 1300;

  x73pos = x73pos - 2;
  document.getElementById('c73').style.left = x73pos;
  if (x73pos < -400) x73pos = 1300;

  x74pos = x74pos - 9;
  document.getElementById('c74').style.left = x74pos;
  if (x74pos < -400) x74pos = 1300;

  x75pos = x75pos - 5;
  document.getElementById('c75').style.left = x75pos;
  if (x75pos < -400) x75pos = 1300;

  x77pos = x77pos - 15;
  document.getElementById('c77').style.left = x77pos;
  if (x77pos < -4000) x77pos = 1000;



  // change speed based on keyboard events
  if (upPressed == 1)
    yspeed = Math.max(yspeed - 1,-1*maxSpeed);
  if (downPressed == 1)
    yspeed = Math.min(yspeed + 1,1*maxSpeed)
  if (rightPressed == 1)
    {
    document.getElementById('character').src="http://s18.postimg.org/3xtiyx5id/car1.png";
    xspeed = Math.min(xspeed + 1,1*maxSpeed);
    }
  if (leftPressed == 1)
    {
    document.getElementById('character').src="http://s18.postimg.org/c5vgjwxet/car1left.png";
    xspeed = Math.max(xspeed - 1,-1*maxSpeed);
    }

  // deceleration
  if (upPressed == 0 && downPressed == 0)
     slowDownY();
  if (leftPressed == 0 && rightPressed == 0)
     slowDownX();

    document.getElementById('energy').innerHTML=energy;
    document.getElementById('time').innerHTML=time;
    
    time = time + 1;

    if (energy == 0)
    {
      loopspeed = 1000000; 
      document.getElementById('end').style.visibility="visible";
    }

    if (energy<0)
    {
      loopspeed = 1000000; 
      document.getElementById('end').style.visibility="visible";
    }

    if (time>10000)
    {
      loopspeed = 1000000; 
      document.getElementById('win').style.visibility="visible";
    }

  // loop
  setTimeout("gameLoop()",loopspeed);
  }
}

function keyDown(e)
{
  var code = e.keyCode ? e.keyCode : e.which;
  if (code == 38)
    upPressed = 1;
  if (code == 40)
    downPressed = 1;
  if (code == 37)
    leftPressed = 1;
  if (code == 39)
    rightPressed = 1;
  if (code == 80)
  {
    if (pause == "n") pause = "y?";
    if (pause == "y") pause = "n?";
  }
}

function keyUp(e)
{
  var code = e.keyCode ? e.keyCode : e.which;
  if (code == 38)
    upPressed = 0;
  if (code == 40)
    downPressed = 0;
  if (code == 37)
    leftPressed = 0;
  if (code == 39)
    rightPressed = 0;
  if (code == 80)
  {
    if (pause == "n?") 
    {
      pause = "n";
      document.getElementById('pause').style.visibility="hidden";
    }
    if (pause == "y?") 
    {
      pause = "y";
      document.getElementById('pause').style.visibility="visible";
    }
    gameLoop();
  }
}